//
//  DCRrivingPhotoController.m
//  Demo
//
//  Created by zhaolin on 2023/1/13.
//

#import "DCAuthDrivingController.h"
#import "DCAuthFooterView.h"
#import "DCTakePhotoController.h"
#import "DCAuthBankCardController.h"

@interface DCAuthDrivingController ()<UITableViewDelegate,UITableViewDataSource,DCAuthFooterViewDelegate,TakePhotoDelegate>

@property (nonatomic, strong) NSData *imageData;

@property (nonatomic, assign) BOOL upLoadImageFinish;

@end

@implementation DCAuthDrivingController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleName:@"Identity documents"];

    // Do any additional setup after loading the view.
}
- (void)configureAlloc {
    [super configureAlloc];
    self.step = @"IDENTITY_INFO";

}

- (void)configureUI {
    [super configureUI];
    [self _setCurrntStep:4];
    [self configureTable];
    [self.tableView registerClass:[DCAuthDrivingCell class] forCellReuseIdentifier:NSStringFromClass(DCAuthDrivingCell.class)];
//    [self.tableView registerClass:[DCAuthContactsInfoCell class] forCellReuseIdentifier:NSStringFromClass(DCAuthContactsInfoCell.class)];
//    [self.tableView registerClass:[DCAuthIdentifyHeader class] forHeaderFooterViewReuseIdentifier:NSStringFromClass(DCAuthIdentifyHeader.class)];
    [self.tableView registerClass:[DCAuthFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass(DCAuthFooterView.class)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topRadiusView.mas_bottom).mas_equalTo(-30);
    }];
    [self.view bringSubviewToFront:self.tableView];
    
}
#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    mWeakSelf;
    DCAuthDrivingCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCAuthDrivingCell.class)];
    cell.callBack = ^(NSString * _Nonnull string) {
        DCTakePhotoController *vc =[DCTakePhotoController new];
        vc.delegate = weakSelf;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    [cell setimageWithImageData:_imageData];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    DCAuthFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(DCAuthFooterView.class)];
    BOOL contentNull = [self allInfoContentNull];
    [footer setCommitBtnEnabled:!contentNull];
    footer.delegate = self;
    return footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 125;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
#pragma mark --- DCAuthFooterViewDelegate
- (void)authFooterViewNextAction:(id _Nullable )sender {
    BOOL contentNull = [self allInfoContentNull];
    if(contentNull) {
        return;
    }
    DCAuthBankCardController *vc =[DCAuthBankCardController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)authFooterViewAgreementAction:(id _Nullable)sender {
    [self pushPrivacyAgreement];
}

#pragma mark - TakePhotoDelegate
- (void)takePhotoComplete:(NSData *)imageData {
    DLog(@"%@",imageData);
    _imageData = imageData;
    [self.tableView reloadData];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DCHttpRequest DCOSSUploadPositiveImage:imageData complate:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        DLog(@"%@",responseObject);
        if (responseObject.code == 200) {
            self.upLoadImageFinish = YES;
            [self.tableView reloadData];
        } else {
            [DCDCToast toastText:responseObject.message];
        }
    }];
}

- (BOOL)allInfoContentNull {
    BOOL contentNull= NO;
    contentNull = !self.upLoadImageFinish;
    return contentNull;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


@interface DCAuthDrivingCell ()
@property (nonatomic, strong) UIImageView *imageV;

@property (nonatomic, strong) UIButton *takePhoto;
@end
@implementation DCAuthDrivingCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configureUI];
    }
    return self;
}
- (void)setimageWithImageData:(NSData *)imageData {
    _imageV.image = imageData ?  [UIImage imageWithData:imageData] : [UIImage imageNamed:@"icon_drving"];
    _takePhoto.hidden = imageData?YES:NO;
}

- (void)configureUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    UILabel *titleLabel = [UILabel new];
    titleLabel.text  = @"Please take a photo of your";
    titleLabel.textColor = [UIColor colorWithString:@"#CED7CD"];
    titleLabel.font = DCFont_PF_Regular(12);
    [self.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.left.mas_equalTo(40);
    }];
    
    UILabel *subTitle = [UILabel new];
    subTitle.text = @"(Driving license)";
    subTitle.textColor = [UIColor colorWithString:@"#658900"];
    subTitle.font = DCFont_PF_Medium(14);
    [self.contentView addSubview:subTitle];
    [subTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(titleLabel.mas_right).mas_offset(10);
        make.centerY.mas_equalTo(titleLabel);
    }];
    
    UIImageView *imageV = [UIImageView new];
    _imageV = imageV;
    imageV.userInteractionEnabled = YES;
    imageV.image = [UIImage imageNamed:@"icon_drving"];
    imageV.contentMode = UIViewContentModeScaleAspectFill;
    imageV.clipsToBounds = YES;
    [self.contentView addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLabel.mas_bottom).mas_offset(30);
        make.right.mas_equalTo(-35);
        make.left.mas_equalTo(35);
        make.height.mas_equalTo(imageV.mas_width).multipliedBy(160.0/300);
        make.bottom.mas_equalTo(-15);
    }];
    
    UIButton *takePhoto = [UIButton buttonWithType:UIButtonTypeCustom];
    _takePhoto = takePhoto;
    [takePhoto setImage:[UIImage imageNamed:@"takePhoto"] forState:UIControlStateNormal];
    [takePhoto addTarget:self action:@selector(takePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
    [imageV addSubview:takePhoto];
    [takePhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(imageV);
        make.centerY.mas_equalTo(imageV);
    }];
    
}

- (void)takePhotoAction:(UIButton *)sender {
    if(self.callBack) {
        self.callBack(@"");
    }
}
@end
